package com.example.es.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.zqm.neu612.Activity.FoodDetailActivity;
import com.example.zqm.neu612.Activity.MainActivity;
import com.example.zqm.neu612.JavaBean.ReturnSuccess;
import com.example.zqm.neu612.JavaBean.User;
import com.example.zqm.neu612.Model.UserModel;
import com.example.zqm.neu612.R;
import com.example.zqm.neu612.Service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;





public class MyFragment extends Fragment {

    private EditText et_Eregusername;
    private EditText et_Erpassword;
    private EditText et_Ermobilephone;
    private EditText et_Eraddress;
    private EditText et_Ercomment;
    private Button btn_certain;

    private int userid;
    private String username;
    private String userpass;
    private String mobilephone;
    private String address;
    private String comment;
    private String u_username;
    private String u_userpass;
    private String u_mobilephone;
    private String u_address;
    private String u_comment;
    String a;
    private int uid;

    UserModel userModel;
    public MyFragment() {
        // Required empty public constructor
        userModel=new UserModel();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);

        showUserInfo();
        et_Eregusername= (EditText)view.findViewById(R.id.et_Eregusername);
        et_Erpassword= (EditText)view.findViewById(R.id.et_Erpassword);
        et_Ermobilephone= (EditText)view.findViewById(R.id.et_Ermobilephone);
        et_Eraddress= (EditText)view.findViewById(R.id.et_Eraddress);
        et_Ercomment= (EditText)view.findViewById(R.id.et_Ercomment);
        btn_certain= (Button)view.findViewById(R.id.btn_certain);

        btn_certain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUpdateInfo();
            }
        });
        return view;
    }


    private void getUpdateInfo() {
        u_username=et_Eregusername.getText().toString();
//        Toast.makeText(EditMineActivity.this,username,Toast.LENGTH_SHORT).show();
        u_userpass=et_Erpassword.getText().toString();
//        Toast.makeText(EditMineActivity.this,u_userpass,Toast.LENGTH_SHORT).show();
        u_mobilephone=et_Ermobilephone.getText().toString();
//        Toast.makeText(EditMineActivity.this,u_mobilephone,Toast.LENGTH_SHORT).show();
        u_address=et_Eraddress.getText().toString();
//        Toast.makeText(EditMineActivity.this,u_address,Toast.LENGTH_SHORT).show();
        u_comment=et_Ercomment.getText().toString();
//        Toast.makeText(EditMineActivity.this,u_comment,Toast.LENGTH_SHORT).show();

        if(u_userpass!=null && "".equals(u_userpass)){
            Toast.makeText(getActivity(),"密码不能为空",Toast.LENGTH_SHORT).show();
        }else {
            if(u_username!=null && "".equals(u_username) && u_mobilephone!=null && "".equals(u_mobilephone) &&u_address!=null && "".equals(u_address) && u_comment!=null && "".equals(u_comment)){
                u_username=username;
                u_mobilephone=mobilephone;
                u_address=address;
                u_comment=comment;
                update();
            }else {
                update();
            }
        }
    }

    private void update() {
        Call call2=userModel.updatee(userid,u_username,u_userpass,u_mobilephone,u_address,u_comment);
        call2.enqueue(new Callback<ReturnSuccess>() {
            @Override
            public void onResponse(Call<ReturnSuccess> call, Response<ReturnSuccess> response) {
                final ReturnSuccess data=response.body();
                String resultes=data.getSuccess();
                if(!resultes.equals("0")){
                    Toast.makeText(getActivity(),"修改信息成功",Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(getActivity(),"修改信息失败",Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onFailure(Call<ReturnSuccess> call, Throwable t) {
                Toast.makeText(getActivity(),"请检查您的网络",Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void showUserInfo() {
        SharedPreferences sharedPreferences=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userid=sharedPreferences.getInt("user_id",2);
        userpass=sharedPreferences.getString("userpass","2");
        Call call =userModel.getUser(userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
          User data=response.body();

                username=data.getUsername();

                et_Eregusername.setText(username);


                et_Erpassword.setText(userpass);

                mobilephone=data.getMobilenum();
                et_Ermobilephone.setText(mobilephone);

                address=data.getAddress();
                et_Eraddress.setText(address);

                comment=data.getComment();
                if (!comment.equals("")){
                    et_Ercomment.setText(comment);}

            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();

            }
        });
    }




}
